<template>
  <div class="login">
    <div class="login_form">
      <img class="title" src="http://likede2-admin.itheima.net/img/logo.595745bd.png" alt="" />
      <el-form ref="ref" :model="form" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="form.loginName" prefix-icon="el-icon-mobile" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" prefix-icon="el-icon-lock" type="password" :show-password="true" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="verification" prop="code">
          <el-input class="verificationLeft" v-model="form.code" prefix-icon="el-icon-key" placeholder="请输入验证码"></el-input>
          <img @click="changeImg" class="verificationRight" :src="verification" alt="" />
        </el-form-item>
        <el-form-item class="tc">
          <el-button type="primary" @click="onLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  data () {
    return {
      form: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        loginType: 0,
        clientToken: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: ['blur', 'change'] },
          { min: 5, max: 11, message: '长度5~11', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
          { pattern: /^\w{5,11}$/, message: '长度5~11', trigger: ['blur', 'change'] }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: ['blur', 'change'] }]
      }
    }
  },
  computed: {
    random: {
      get () {
        return this.form.clientToken || Math.floor(Math.random() * 100000)
      },
      set (val) {
        this.form.clientToken = val
      }
    },
    verification () {
      return `https://likede2-java.itheima.net/api/user-service/user/imageCode/${this.random}`
    }
  },
  methods: {
    changeImg () {
      this.random = Math.floor(Math.random() * 100000)
    },
    async onLogin () {
      this.form.clientToken = this.random + ''
      // const res = await login(this.form)
      // console.log(res)
      try {
        const valid = await this.$refs.ref.validate
        if (valid) {
          const data = await login(this.form)
          console.log(data)
          if (data.msg === '登录成功') {
            this.$store.commit('user/setUserToken', data)
            this.$router.push('/')
            return 0
          }
          this.$message({
            message: data.msg,
            type: 'warning'
          })
          if (data.msg === '验证码错误') {
            this.changeImg()
            this.form.code = ''
            this.$refs.ref.resetFields()
          }
        }
      } catch (error) {
        console.log(error)
      }
    }
  },
  async created () {}
}
</script>

<style lang="less" scoped>
.login {
  background-image: url(http://likede2-admin.itheima.net/img/background.be4fae7d.png);
  position: relative;
  height: 1007px;
  width: 100%;
  overflow: hidden;
  background-repeat: no-repeat;
  background-size: cover;
}
.login_form {
  position: absolute;
  width: 518px;
  height: 388px;
  top: 50%;
  left: 50%;
  margin-top: -194px;
  margin-left: -259px;
  padding: 76px 35px 0;
  background: #fff;
  -webkit-box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
  box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
  border-radius: 10px;
  width: 448px;
  height: 312px;
  .title {
    position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
  }
  /deep/ .el-input__inner {
    line-height: 40px;
    position: relative;
    font-size: 14px;
    height: 52px;
    padding-left: 40px;
    color: #889aa4;
  }
  /deep/ .el-input__prefix {
    padding: 3px 5px 0px 4px;
    color: #889aa4;
    font-size: 16px;
    width: 30px;
  }
  /deep/ .el-button--primary {
    width: 100%;
    height: 52px;
    background: linear-gradient(262deg, #2e50e1, #6878f0);
    opacity: 0.91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
  }
  /deep/ .el-form-item__content {
    height: 52px;
  }
  .verificationLeft {
    position: absolute;
    top: 0;
    width: 70%;
  }
  .verificationRight {
    position: absolute;
    right: 0;
    width: 30%;
  }
}
</style>
